package com.journaldev.spring;

import java.sql.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.journaldev.spring.model.Medicine;
import com.journaldev.spring.model.UserInfo;
import com.journaldev.spring.model.Ward;
import com.journaldev.spring.service.MedicineService;
import com.journaldev.spring.service.UserInfoService;
import com.journaldev.spring.util.SimpleDateTimeEditor;
import com.journaldev.spring.util.SimpleUserEditor;
import com.journaldev.spring.util.SimpleWardEditor;

@Controller
public class MedicineController {

	private MedicineService medicineService;
	
	private UserInfoService userInfoService;
	
	@Autowired(required=true)
	@Qualifier(value="userInfoService")
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	@Autowired(required=true)
	@Qualifier(value="medicineService")
	public void setMedicineService(MedicineService medicineService) {
		this.medicineService = medicineService;
	}
	
	@InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        binder.registerCustomEditor(UserInfo.class, new SimpleUserEditor(userInfoService));
    }
	
	@RequestMapping(value = {"/medicines"}, method = RequestMethod.GET)
	public String listCities(HttpServletRequest request,Model model) {
		UserInfo user = (UserInfo) request.getSession().getAttribute("USER_LOGIN");
		model.addAttribute("medicine", new Medicine());
		model.addAttribute("listMedicines", this.medicineService.listMedicine());
		model.addAttribute("id_user", user.getId());
		return "medicine";
	}
	
	//For add and update person both
	@RequestMapping(value= "/medicine/add", method = RequestMethod.POST)
	public String addMedicine(@ModelAttribute("medicine") Medicine p){
		
		if(p.getId() == 0){
			//new person, add it
			this.medicineService.addMedicine(p);
		}else{
			//existing person, call update
			this.medicineService.updateMedicine(p);
		}
		
		return "redirect:/medicines";
		
	}
	
	@RequestMapping("/medicine/remove/{id}")
    public String removeMedicine(@PathVariable("id") int id){
		
        this.medicineService.removeMedicine(id);
        return "redirect:/medicines";
    }
 
    @RequestMapping("/medicine/edit/{id}")
    public String editMedicine(@PathVariable("id") int id, Model model, HttpServletRequest request){
    	UserInfo user = (UserInfo) request.getSession().getAttribute("USER_LOGIN");
        model.addAttribute("medicine", this.medicineService.getMedicineById(id));
        model.addAttribute("listMedicines", this.medicineService.listMedicine());
        model.addAttribute("id_user", user.getId());
        return "medicine";
    }
	
}
